﻿using System;
using System.Linq;
using System.Text;
using EzDesk.Apps.Data.ProjectModule;
using EzDesk.Apps.Model.ProjectModule;
using EzDesk.Core.Model;
using EzDesk.Utility;
using EzDesk.Utility.Extensions;

namespace EzDesk.Apps.ServiceImpl.ProjectModule.History
{
    /// <summary>
    /// 项目中有Task发生变化时记录项目历史
    /// </summary>
    public class ContentUpdatedTaskHistoryRecorder : TaskHistoryRecorder
    {
        protected Task OldTask { get; set; }
        public ContentUpdatedTaskHistoryRecorder(Task oldTask, Task newTask, Guid? senderId)
        {
            OldTask = oldTask;
            innerInitializeTaskHistory(newTask, senderId, "ContentUpdatedTaskHistory");
        }

        protected override string GetTaskContent()
        {
            int rowCount = 0;
            var sb = new StringBuilder();
            sb.AppendLine("{0} 更新了项目计划 \"{1}\" ".FormatString(Sender.Name, OldTask.Name));

            if (OldTask.Name != Task.Name)
            {
                sb.AppendLine("名称修改为 \"{0}\"".FormatString(Task.Name));
                rowCount++;
            }
            if (OldTask.ChargePersonId != Task.ChargePersonId && Task.ChargePersonId.HasValue)
            {
                var chargePerson = EzApiClient.GetSimpleUserById(Task.ChargePersonId.Value);
                sb.AppendLine("负责人修改为 {0}".FormatString(chargePerson.Name));
                rowCount++;
            }
            if (Task.StartOn.HasValue && (OldTask.Duration != Task.Duration || OldTask.StartOn != Task.StartOn))
            {
                sb.AppendLine("计划周期修改为 {0} 至 {1} ".FormatString(
                    Task.StartOn.Value.ToFullDate(),
                    Task.StartOn.Value.AddDays(Math.Round(Task.Duration)/8 - 1).ToFullDate()
                                  ));
                rowCount++;
            }
            return rowCount > 1 ? GetMuiltipleLineContent(sb) : sb.ToString();
        }
    }

    public class TaskExpiredHistoryRecorder : TaskHistoryRecorder
    {
        public TaskExpiredHistoryRecorder(Guid taskId)
            : base(taskId, null, "TaskExpiredHistory")
        {
        }
    
        protected override string  GetTaskContent()
        {
            return "\"{0}\" 项目计划已过期".FormatString(Task.Name);
        }
    }

    /// <summary>
    /// 项目中有一个新的Task被删除的时候记录的项目历史
    /// </summary>
    public class DeleteTaskHistoryRecorder : TaskHistoryRecorder
    {
        public DeleteTaskHistoryRecorder(Guid taskId, Guid? senderId)
            : base(taskId, senderId, "DeleteTaskHistory")
        {
        }

        protected override string GetTaskContent()
        {
            return "{0} 删除了项目计划 \"{1}\"".FormatString(Sender.Name, Task.Name);
        }
    }

    /// <summary>
    /// 项目中有一个新的Task被创建的时候记录的项目历史
    /// </summary>
    public class CreateTaskHistoryRecorder : TaskHistoryRecorder
    {
        public CreateTaskHistoryRecorder(Guid taskId, Guid? senderId)
            : base(taskId, senderId, "CreateTaskHistory")
        {
        }

        protected override string GetTaskContent()
        {
            return "{0} 创建了项目计划 \"{1}\"".FormatString(Sender.Name, Task.Name);
        }
    }

    public abstract class TaskHistoryRecorder : HistoryRecorderBase
    {
        private ProjectHistory _projectHistory = new ProjectHistory();

        public override ProjectHistory ProjectHistory
        {
            get { return _projectHistory; }
        }

        protected Guid TaskId { get; set; }
        protected Task Task { get; set; }
        protected Guid? SenderId { get; set; }
        protected SimpleUser Sender { get; set; }

        protected TaskHistoryRecorder()
        {
        }

        protected TaskHistoryRecorder(Guid taskId, Guid? senderId, string categoryName)
        {
            initializeHistory(taskId, senderId, categoryName);
        }

        protected void innerInitializeTaskHistory(Task task, Guid? senderId, string categoryName)
        {
            Task = task;
            SenderId = senderId;
            if (senderId.HasValue)
            {
                Sender = EzApiClient.GetSimpleUserById(SenderId.Value);
            }

            _projectHistory = new ProjectHistory()
            {
                Id = Guid.NewGuid(),
                CreateOn = DateTime.Now,
                HistoryType = HistoryType.Task,
                ProjectId = Task.ProjectId,
                SenderId = senderId,
                LinkUrl = GetTaskLinkUrl(),
                Content = GetTaskContent(),
                CategoryName = categoryName,
            };
        }

        protected void initializeHistory(Guid taskId, Guid? senderId, string categoryName)
        {
            TaskId = taskId;
            var task = EzDeskProjectContainer.Go(
                container => container.TaskSet.FirstOrDefault(p => p.Id == TaskId));
            innerInitializeTaskHistory(task, senderId, categoryName);
        }

        protected abstract string GetTaskContent();

        private string GetTaskLinkUrl()
        {
            // todo: 这个workitem有可能会显示不出来的，因为这个workitem并不一定是“自己的”Workitem
            return "/project/projectTasks/{0}".FormatString(TaskId.ToString());
        }
    }
}